package com.qly.website.controller;

import com.qly.website.core.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Andy ye on 2018/8/3.
 */

@RestController
@RequestMapping(value = "/file")
public class FileController {


    @Autowired
    FileUtil fileUtil;

    @RequestMapping(value = "/add.html")
    public ModelAndView filePage(ModelAndView model) {
        model.setViewName("/file/addOne");
        return model;
    }

    @RequestMapping(value = "/batch_add.html")
    public ModelAndView MutiFilePage(ModelAndView model) {
        model.setViewName("/file/addMuti");
        return model;
    }

    @RequestMapping("/upload")
    @ResponseBody
    public String handleFileUpload(@RequestParam("file") MultipartFile file) {
        return fileUtil.fileUpload(file);
    }

    @RequestMapping(value = "/batch/upload", method = RequestMethod.POST)
    @ResponseBody
    public String handleFileUpload(HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        return fileUtil.mutiFileUpload(files);
    }
}
